﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.Linq.Mapping;
using System.Reflection;
using System.Data.SqlClient;
using System.Web;
using System.Net;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using System.Threading;
using System.IO;


namespace aliformerp
{
    public partial class searchform : Form
    {
        public static string apitype;
        public static DataTable dt;
        public static string picstr = "ept";
        //  public static int imgno=0;
        public searchform()
        {
            InitializeComponent();
            richTextBox1.Text += "你获取的令牌返回是:" + userinfo.acctk2.access_token;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            apitype = "offer.search";
            Dictionary<string, string> parm1 = new Dictionary<string, string>();
            parm1.Add("province", "浙江");
            parm1.Add("memberId", "wyshengtuo");
            string geturiout = userinfo.uribuider(apitype, parm1, true);
            string returnhtml = userinfo.gethttpwithuri(geturiout);
            returnhtml = htmltodatatable(returnhtml);
            // parm1.Add("", "");
        }

        private string htmltodatatable(string returnhtml)
        {
            returnhtml = returnhtml.Substring(returnhtml.IndexOf("[{") + 2);
            richTextBox1.Text += returnhtml;
            string[] regsuzu = returnhtml.Split(new string[] { "},{" }, StringSplitOptions.None);
            string g = "(\\w+)\":({\"|([^,]+)|\"([^\"]+)\")";
            int suzucnt = regsuzu.Count();
            int suzumaxindx = 0;
            int mccntmax = 0;
            for (int i = 0; i < suzucnt; i++)
            {
                string r = g;
                MatchCollection mc = Regex.Matches(regsuzu[i], r);
                if (mc.Count > mccntmax)
                {
                    mccntmax = mc.Count;
                    suzumaxindx = i;
                }
            }

            //   MessageBox.Show(a.ToString());
            DataTable tb = new DataTable();
            tb.TableName = "test1";
            string r3 = g;
            MatchCollection mca = Regex.Matches(regsuzu[suzumaxindx], r3);
            List<string> tmp = new List<string>();
            //List<int> lint = new List<int>();
            //int mcct = mc.Count;
            //lint.Add(mcct);
            for (int i = 0; i < mccntmax; i++)
            {
                var dc = new DataColumn();
                string strRow = mca[i].Value;
                string[] strRows = strRow.Split(':');
                if (tmp.IndexOf(strRows[0]) == -1)
                {
                    tmp.Add(strRows[0]);
                    dc.ColumnName = strRows[0];
                    tb.Columns.Add(dc);
                }
                else
                {
                    strRows[0] += "a" + i.ToString();
                    tmp.Add(strRows[0]);
                    dc.ColumnName = strRows[0];
                    tb.Columns.Add(dc);
                }
            }
            tb.AcceptChanges();

            foreach (string regstr in regsuzu)
            {
                string r2 = g;
                MatchCollection mc2 = Regex.Matches(regstr, r2);
                DataRow dr = tb.NewRow();
                for (int i = 0; i < mc2.Count; i++)
                {
                    string strRow = mc2[i].Value;
                    string strRows = strRow.Substring(strRow.IndexOf(":") + 1);
                    dr[i] = strRows.ToString();

                }
                tb.Rows.Add(dr);
                // lint.Add(mc2.Count);
            }
            tb.AcceptChanges();
            //if (lint.Max() - mcct > 2)
            //{
            //    DataTable tb2 = new DataTable();
            //    for (int i = 0; i < lint.Max(); i++)
            //    {
            //        var dc = new DataColumn();



            //        dc.ColumnName = i.ToString();
            //        tb2.Columns.Add(dc);

            //    }
            //    tb2.AcceptChanges();
            //    foreach (string regstr in regsuzu)
            //    {
            //        string r2 = "(\\w+)\":({\"|([^\"]+)(?=,)|\"([^\"]+)\")";
            //        MatchCollection mc2 = Regex.Matches(regstr, r2);
            //        DataRow dr = tb.NewRow();
            //        for (int i = 0; i < mcct; i++)
            //        {
            //            string strRow = mc2[i].Value;
            //            string strRows = strRow.Substring(strRow.IndexOf(":") + 1);
            //            dr[i] = strRows.ToString();

            //        }
            //        tb2.Rows.Add(dr);

            //    }
            //    tb2.AcceptChanges();
            //    dataGridView1.DataSource = tb2;
            //}
            //else
            //{
            dataGridView1.DataSource = tb;

            return returnhtml;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            apitype = e.Node.Name.ToString();
            string apitypetablename = apitype.Replace(".", "dot");

            getLinqCommonTable(apitypetablename);

        }
        private void getLinqCommonTable(string sTableName)
        {
            listView1.Items.Clear();
            // Type theObj;
            //  ArrayList list = new ArrayList();
            //  Type tp = Assembly.Load("aliformerp").
            //int a = tp.Count();
            //foreach (Type t in tp)
            //{
            //    if (t.Name == "offerdotsearch")
            //    {
            //         theObj = t;
            //    }
            //}
            //   //     FieldInfo[] minf = t.GetFields();
            //   //     int a3 = 1;
            //   //foreach (FieldInfo mf in minf)
            //   //{
            //   //    DataTable eq = mf.GetValue(theObj) as DataTable;
            //   //    dataGridView2.DataSource = eq;
            //   //     }
            //        PropertyInfo[] minf = t.GetProperties();
            //   int a3 = 1;
            //   foreach (PropertyInfo mf in minf)
            //   {
            //       if (mf.Name==sTableName)
            //       {
            //           DataTable eq = mf.GetValue(theObj,null) as DataTable;
            //       dataGridView2.DataSource = eq;
            //       }

            //   }
            SqlConnection sn = new SqlConnection(Form1.dt.Connection.ConnectionString);
            sn.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from " + sTableName, sn);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            sn.Close();
            //     int a2=  minf.Count();
            //
            dataGridView2.DataSource = dt;

            foreach (DataRow dr in dt.Rows)
            {
                if (dr[0].ToString() == "returnFields")
                {
                    string[] flds = dr[1].ToString().Split(',');
                    foreach (string fl in flds)
                    {
                        ListViewItem lt = new ListViewItem();
                        lt.Text = fl;
                        listView1.Items.Add(lt);
                    }

                }

            }
            sn.Close();
            //    }


            // MessageBox.Show(tp.GetMembers().Count().ToString());

            //   theObj.GetType(sTableName);
            //   Type t = Type.GetType(String.Format("{0}{1}", sNamespace, sTableName), true, true);
            // var temp = Activator.CreateInstance(t);

            // var q = Form1.dt.ExecuteQuery(temp.GetType(), "select * from DepartmentShip");
            // 
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" & textBox2.Text != "" & textBox1.Text != null & textBox2.Text != null)
            {
                if (dataGridView1.DataSource == null & (dataGridView1.RowCount > 0 & dataGridView1.ColumnCount > 0))
                {
                    dataGridView1.Rows.Clear();
                    dataGridView1.Columns.Clear();
                }
                if (dataGridView1.DataSource != null)
                {
                    dataGridView1.DataSource = null;
                }
                DataTable se = new DataTable();
                se.TableName = "tesettbo";

                DataColumn dc = new DataColumn();
                dc.ColumnName = "pic";
                DataColumn dc2 = new DataColumn();
                dc2.ColumnName = "productdescription";
                DataColumn dc3 = new DataColumn();
                dc3.ColumnName = "comanyname";
                DataColumn dc4 = new DataColumn();
                dc4.ColumnName = "offerid";
                DataColumn dc5 = new DataColumn();
                dc5.ColumnName = "memberid";
                DataColumn dc6 = new DataColumn();
                dc6.ColumnName = "offeruri";
                se.Columns.Add(dc);
                se.Columns.Add(dc2);
                se.Columns.Add(dc3);
                se.Columns.Add(dc4);
                se.Columns.Add(dc5);
                se.Columns.Add(dc6);
                int start1 = int.Parse(textBox1.Text);
                int end1 = int.Parse(textBox2.Text);
                apitype = "all.search";
                for (int ia = start1; ia < end1; ia++)
                {

                    DataTable dto = GetDgvToTable(dataGridView2);
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][0].ToString() == "beginPage")
                        {
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), ia.ToString().Trim());
                            continue;
                        }
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                        if (dto.Rows[i][0].ToString() == "keywords")
                        {
                            picstr = dto.Rows[i][1].ToString();
                        }

                    }
                    string urils = userinfo.sarchkeyword(parm1);
                    richTextBox1.Text += urils;
                    string urilsout = userinfo.gethttpwithurigbk(urils);
                    DataTable thedt = htmlkeywordtodatatable(urilsout);
                    richTextBox1.Text += urilsout;
                    //  dataGridView1.DataSource = thedt;
                    foreach (DataRow daw in thedt.Rows)
                    {
                        se.Rows.Add(daw.ItemArray);
                    }
                }
                dataGridView1.DataSource = se;
                return;
            }
            if (apitype == "all.search")
            {
                if (dataGridView1.DataSource == null & (dataGridView1.RowCount > 0 & dataGridView1.ColumnCount > 0))
                {
                    dataGridView1.Rows.Clear();
                    dataGridView1.Columns.Clear();
                    //  MessageBox.Show("a");
                    DataTable dto = GetDgvToTable(dataGridView2);
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                        if (dto.Rows[i][0].ToString() == "keywords")
                        {
                            picstr = dto.Rows[i][1].ToString();
                        }
                    }
                    string urils = userinfo.sarchkeyword(parm1);
                    richTextBox1.Text += urils;
                    string urilsout = userinfo.gethttpwithurigbk(urils);
                    DataTable thedt = htmlkeywordtodatatable(urilsout);
                    richTextBox1.Text += urilsout;
                    dataGridView1.DataSource = thedt;
                    return;
                }
                else if (dataGridView1.DataSource != null)
                {
                    dataGridView1.DataSource = null;
                    //  MessageBox.Show("a");
                    DataTable dto = GetDgvToTable(dataGridView2);
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                        if (dto.Rows[i][0].ToString() == "keywords")
                        {
                            picstr = dto.Rows[i][1].ToString();
                        }
                    }
                    string urils = userinfo.sarchkeyword(parm1);
                    richTextBox1.Text += urils;
                    string urilsout = userinfo.gethttpwithurigbk(urils);
                    DataTable thedt = htmlkeywordtodatatable(urilsout);
                    richTextBox1.Text += urilsout;
                    dataGridView1.DataSource = thedt;
                    return;
                }
                else
                {

                    //  MessageBox.Show("a");
                    DataTable dto = GetDgvToTable(dataGridView2);
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")

                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                        if (dto.Rows[i][0].ToString() == "keywords")
                        {
                            picstr = dto.Rows[i][1].ToString();
                        }
                    }
                    string urils = userinfo.sarchkeyword(parm1);
                    richTextBox1.Text += urils;
                    string urilsout = userinfo.gethttpwithurigbk(urils);
                    DataTable thedt = htmlkeywordtodatatable(urilsout);
                    richTextBox1.Text += urilsout;
                    dataGridView1.DataSource = thedt;
                    return;
                }
            }
            else
            {
                if (dataGridView1.DataSource == null & (dataGridView1.RowCount > 0 & dataGridView1.ColumnCount > 0))
                {
                    dataGridView1.Rows.Clear();
                    dataGridView1.Columns.Clear();
                    DataTable dto = GetDgvToTable(dataGridView2);
                    //  int a = dto.Columns.Count;
                    //  string optionstr = "";
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                    }
                    string list = "";
                    for (int i = 0; i < this.listView1.CheckedItems.Count; i++)
                    {
                        if (this.listView1.CheckedItems[i].Checked)
                        {
                            list += this.listView1.CheckedItems[i].Text + ",";
                        }
                    }
                    if (list != "")
                    {
                        parm1.Add("returnFields", list.TrimEnd(','));
                    }

                    string geturiout = userinfo.uribuider(apitype, parm1, true);
                    richTextBox2.Text += geturiout;
                    string returnhtml = userinfo.gethttpwithuri(geturiout);
                    returnhtml = htmltodatatable(returnhtml);
                }
                else if (dataGridView1.DataSource != null)
                {
                    dataGridView1.DataSource = null;
                    DataTable dto = GetDgvToTable(dataGridView2);
                    //  int a = dto.Columns.Count;
                    //  string optionstr = "";
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                    }
                    string list = "";
                    for (int i = 0; i < this.listView1.CheckedItems.Count; i++)
                    {
                        if (this.listView1.CheckedItems[i].Checked)
                        {
                            list += this.listView1.CheckedItems[i].Text + ",";
                        }
                    }
                    if (list != "")
                    {
                        parm1.Add("returnFields", list.TrimEnd(','));
                    }

                    string geturiout = userinfo.uribuider(apitype, parm1, true);
                    richTextBox2.Text += geturiout;
                    string returnhtml = userinfo.gethttpwithuri(geturiout);
                    returnhtml = htmltodatatable(returnhtml);
                }
                // MessageBox.Show(   Form1.dt.;
                else
                {



                    DataTable dto = GetDgvToTable(dataGridView2);
                    //  int a = dto.Columns.Count;
                    //  string optionstr = "";
                    Dictionary<string, string> parm1 = new Dictionary<string, string>();
                    for (int i = 0; i < dto.Rows.Count; i++)
                    {
                        if (dto.Rows[i][1].ToString() != null & dto.Rows[i][1].ToString() != "" & dto.Rows[i][0].ToString() != "returnFields")
                            parm1.Add(dto.Rows[i][0].ToString().Trim(), dto.Rows[i][1].ToString().Trim());
                    }
                    string list = "";
                    for (int i = 0; i < this.listView1.CheckedItems.Count; i++)
                    {
                        if (this.listView1.CheckedItems[i].Checked)
                        {
                            list += this.listView1.CheckedItems[i].Text + ",";
                        }
                    }
                    if (list != "")
                    {
                        parm1.Add("returnFields", list.TrimEnd(','));
                    }

                    string geturiout = userinfo.uribuider(apitype, parm1, true);
                    richTextBox2.Text += geturiout;
                    string returnhtml = userinfo.gethttpwithuri(geturiout);
                    returnhtml = htmltodatatable(returnhtml);
                    // MessageBox.Show(optionstr.ToString());
                }
            }
        }
        private void searchform_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“myalidbDataSet2.offerdotget”中。您可以根据需要移动或删除它。
            //this.offerdotgetTableAdapter.Fill(this.myalidbDataSet2.offerdotget);
            //// TODO: 这行代码将数据加载到表“myalidbDataSet1.offerdotsearch”中。您可以根据需要移动或删除它。
            //this.offerdotsearchTableAdapter.Fill(this.myalidbDataSet1.offerdotsearch);
            //// TODO: 这行代码将数据加载到表“myalidbDataSet.offersearch”中。您可以根据需要移动或删除它。
            //this.offersearchTableAdapter.Fill(this.myalidbDataSet.offersearch);

        }

        private void trueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView2.CurrentCell.Value = "true";
        }



        private void falseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView2.CurrentCell.Value = "false";
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }
        public static DataTable GetDgvToTable(DataGridView dgv)
        {
            DataTable dtbl_QianTai = new DataTable();
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                dtbl_QianTai.Columns.Add(dc);
            }
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dtbl_QianTai.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    if (dgv.Rows[count].Cells[countsub].Value != null)
                    {
                        dr[countsub] = dgv.Rows[count].Cells[countsub].Value.ToString();
                    }
                    else
                    {
                        dr[countsub] = "";
                    }
                }
                dtbl_QianTai.Rows.Add(dr);
            }
            return dtbl_QianTai;
        }

        private void button3_Click(object sender, EventArgs e)
        {

            //   DataGridView dgvout =       changedatagridwiewdtopic(ref dataGridView1);
            dt = GetDgvToTable(dataGridView1);
            dataGridView1.DataSource = null;
            //  dataGridView1 = new DataGridView();
            int imgno = 0;
            for (int cmc = 0; cmc < dt.Columns.Count; cmc++)
            {
                if (dt.Rows[0][cmc].ToString().ToLower().Contains("http") & (dt.Rows[0][cmc].ToString().ToLower().Contains("jpg") | dt.Rows[0][cmc].ToString().ToLower().Contains("png")))
                {
                    DataGridViewImageColumn imgcl = new DataGridViewImageColumn();
                    imgcl.Name = dt.Columns[cmc].ColumnName.ToString();
                    dataGridView1.Columns.Add(imgcl);
                }
                else
                {
                    DataGridViewTextBoxColumn txcl = new DataGridViewTextBoxColumn();
                    txcl.Name = dt.Columns[cmc].ColumnName.ToString();
                    dataGridView1.Columns.Add(txcl);
                }

            }
            for (int count = 0; count < dt.Rows.Count; count++)
            {
                DataGridViewRow dgr = new DataGridViewRow();
                dgr.CreateCells(dataGridView1);
                for (int countsub = 0; countsub < dt.Columns.Count; countsub++)
                {
                    if (dt.Rows[count][countsub].ToString().ToLower().Contains("http") & (dt.Rows[count][countsub].ToString().ToLower().Contains("jpg") | dt.Rows[count][countsub].ToString().ToLower().Contains("png")))
                    {


                        string oldu = dt.Rows[count][countsub].ToString().ToLower();

                        string oldunew = oldu.Contains("\"") ? oldu.Substring(oldu.IndexOf("http"), oldu.IndexOf("\"", oldu.IndexOf("http")) - oldu.IndexOf("http")).Trim() : oldu.Substring(oldu.IndexOf("http"));
                        Thread th = new Thread(() =>
                        {
                            try
                            {
                                if (File.Exists(picstr + imgno.ToString() + ".png"))
                                {
                                    FileInfo file = new FileInfo(picstr + imgno.ToString() + ".png");
                                    if (file.Length > 80000)
                                    {
                                        Image mg = System.Drawing.Image.FromFile("pictd.png");
                                        DataGridViewImageCell dgcc = new DataGridViewImageCell();
                                        dgcc.ImageLayout = DataGridViewImageCellLayout.Zoom;

                                        dgcc.Value = mg;
                                        dgr.Cells[countsub] = dgcc;
                                        //  mg.Dispose();
                                    }
                                    else
                                    {
                                        Image mg = System.Drawing.Image.FromFile(picstr + imgno.ToString() + ".png");
                                        DataGridViewImageCell dgcc = new DataGridViewImageCell();
                                        dgcc.ImageLayout = DataGridViewImageCellLayout.Zoom;

                                        dgcc.Value = mg;
                                        dgr.Cells[countsub] = dgcc;
                                        //   mg.Dispose();
                                    }
                                }
                                else
                                {
                                    WebClient wc = new WebClient();
                                    wc.DownloadFile(oldunew, picstr + imgno.ToString() + ".png");

                                    FileInfo file = new FileInfo(picstr + imgno.ToString() + ".png");
                                    if (file.Length > 80000)
                                    {
                                        Image mg = System.Drawing.Image.FromFile("pictd.png");
                                        DataGridViewImageCell dgcc = new DataGridViewImageCell();
                                        dgcc.ImageLayout = DataGridViewImageCellLayout.Zoom;

                                        dgcc.Value = mg;
                                        dgr.Cells[countsub] = dgcc;
                                        //mg.Dispose();
                                    }
                                    else
                                    {
                                        Image mg = System.Drawing.Image.FromFile(picstr + imgno.ToString() + ".png");
                                        DataGridViewImageCell dgcc = new DataGridViewImageCell();
                                        dgcc.ImageLayout = DataGridViewImageCellLayout.Zoom;

                                        dgcc.Value = mg;
                                        dgr.Cells[countsub] = dgcc;
                                        //  mg.Dispose();
                                    }
                                }


                            }
                            catch (System.Exception ex)
                            {
                                this.BeginInvoke((ThreadStart)delegate()
                                {
                                    richTextBox2.Text += ex.ToString();
                                });
                                return;
                            }

                        });
                        th.Start();
                        th.Join();
                        imgno += 1;
                    }
                    else
                    {
                        DataGridViewTextBoxCell dgcc = new DataGridViewTextBoxCell();
                        dgcc.Value = dt.Rows[count][countsub].ToString();
                        dgr.Cells[countsub] = dgcc;

                        // dataGridView1.Rows[count].Cells[countsub].Value 
                    }

                }
                dataGridView1.Rows.Add(dgr);
            }
            //  dataGridView1 = dgvout;
            //string list = "";
            //for (int i = 0; i < this.listView1.CheckedItems.Count; i++)
            //{
            //    if (this.listView1.CheckedItems[i].Checked)
            //    {
            //        list += this.listView1.CheckedItems[i].Text + ",";
            //    }
            //}
            ////MessageBox.Show(list.TrimEnd(','));
            //ListViewItem a = new ListViewItem();
            //a.Text = "foranew";
            //listView1.Items.Add(a);


            //for (int count = 0; count < dataGridView1.Rows.Count; count++)
            //{
            //    for (int countsub = 0; countsub < dataGridView1.Columns.Count; countsub++)
            //    {
            //        if (dataGridView1.Rows[count].Cells[countsub].Value != null)
            //        {

            //            string rctopic = dataGridView1.Rows[count].Cells[countsub].Value.ToString().ToLower();
            //            if (rctopic.Contains("http"))
            //            {
            //                string oldu = rctopic;
            //                string oldunew = oldu.Substring(oldu.IndexOf("http"), oldu.IndexOf("\"", oldu.IndexOf("http")) - oldu.IndexOf("http")).Trim();
            //            WebClient wc = new WebClient();
            //            wc.DownloadFile(oldunew, imgno.ToString() + ".png");
            //        Image mg =    System.Drawing.Image.FromFile(imgno.ToString() + ".png");
            //                dataGridView1.Rows[count].Cells[countsub].Value = mg;
            //             //   imgno+=1;
            //            }
            //            else
            //            {
            //                continue;
            //            }
            //        }
            //    }
            //}
        }
        public DataGridView changedatagridwiewdtopic(ref DataGridView dgv)
        {


            DataTable dt = GetDgvToTable(dgv);
            dgv = new DataGridView();
            int imgno = 0;
            for (int cmc = 0; cmc < dt.Columns.Count; cmc++)
            {
                if (dt.Rows[0][cmc].ToString().ToLower().Contains("http"))
                {
                    DataGridViewImageColumn imgcl = new DataGridViewImageColumn();

                    dgv.Columns.Add(imgcl);
                }
                else
                {
                    DataGridViewTextBoxColumn txcl = new DataGridViewTextBoxColumn();
                    dgv.Columns.Add(txcl);
                }

            }
            for (int count = 0; count < dt.Rows.Count; count++)
            {
                DataGridViewRow dgr = new DataGridViewRow();
                dgr.CreateCells(dgv);
                for (int countsub = 0; countsub < dt.Columns.Count; countsub++)
                {
                    if (dt.Rows[count][countsub].ToString().ToLower().Contains("http"))
                    {


                        string oldu = dt.Rows[count][countsub].ToString().ToLower();
                        string oldunew = oldu.Substring(oldu.IndexOf("http"), oldu.IndexOf("\"", oldu.IndexOf("http")) - oldu.IndexOf("http")).Trim();
                        Thread th = new Thread(() =>
                        {
                            WebClient wc = new WebClient();
                            wc.DownloadFile(oldunew, imgno.ToString() + ".png");
                            Image mg = System.Drawing.Image.FromFile(imgno.ToString() + ".png");
                            DataGridViewImageCell dgcc = new DataGridViewImageCell();
                            dgcc.Value = mg;
                            dgr.Cells[countsub] = dgcc;
                        });
                        th.Start();
                        th.Join();
                        imgno += 1;
                    }
                    else
                    {
                        DataGridViewTextBoxCell dgcc = new DataGridViewTextBoxCell();
                        dgcc.Value = dt.Rows[count][countsub].ToString();
                        dgr.Cells[countsub] = dgcc;

                        // dgv.Rows[count].Cells[countsub].Value 
                    }

                }
                dgv.Rows.Add(dgr);
            }
            return dgv;
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 打开当前选定uriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // MessageBox.Show(dataGridView1.CurrentCell.ValueType.ToString());
            if (dataGridView1.CurrentCell.ValueType == typeof(System.Drawing.Image))
            {
                int ridxa = dataGridView1.CurrentCell.RowIndex;
                int cidxa = dataGridView1.CurrentCell.ColumnIndex;
                string openturi = dt.Rows[ridxa][cidxa].ToString().ToLower().Replace("\"", "").Trim();
                if (openturi.StartsWith("http"))
                {
                    webb.toopenuriis = openturi;

                    webb w1 = new webb(this);
                    this.Hide();
                    w1.Show();

                }
            }
            else
            {
                string openturi = dataGridView1.CurrentCell.Value.ToString().ToLower().Replace("\"", "").Trim();
                openturi = openturi.Substring(openturi.IndexOf("http"));
                if (openturi.StartsWith("http"))
                {
                    webb.toopenuriis = openturi;

                    webb w1 = new webb(this);
                    this.Hide();
                    w1.Show();

                }

            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();
            dataGridView1.DataSource = dt;
            //for (int i = 0; i < imgno;i++ )
            //{
            //    if (File.Exists(i.ToString()+".png"))
            //    {
            //        File.Delete(i.ToString() + ".png");
            //    }
            //}
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public DataTable htmlkeywordtodatatable(string org)
        {
            DataTable dtl = new DataTable();
            dtl.TableName = "keywordsearch";
            DataColumn dc = new DataColumn();
            dc.ColumnName = "pic";
            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "productdescription";
            DataColumn dc3 = new DataColumn();
            dc3.ColumnName = "comanyname";
            DataColumn dc4 = new DataColumn();
            dc4.ColumnName = "offerid";
            DataColumn dc5 = new DataColumn();
            dc5.ColumnName = "memberid";
            DataColumn dc6 = new DataColumn();
            dc6.ColumnName = "offeruri";
            dtl.Columns.Add(dc);
            dtl.Columns.Add(dc2);
            dtl.Columns.Add(dc3);
            dtl.Columns.Add(dc4);
            dtl.Columns.Add(dc5);
            dtl.Columns.Add(dc6);
            dtl.AcceptChanges();
            //string r = "href=\"(http://detail.1688.com/offer/\\d+.html)";
            //string r2 = "alt=\"([^\"]+)\"";
            //string r3 = "target=\"_blank\" >([^\"]+)</a>";
            //string r4 = "href=\"http://detail.1688.com/offer/(\\d+).html";
            //MatchCollection match = Regex.Matches(org, r);
            //MatchCollection match2 = Regex.Matches(org, r2);
            //MatchCollection match3 = Regex.Matches(org, r3);
            //MatchCollection match4 = Regex.Matches(org, r4);
            //List<int> lt = new List<int>();
            //lt.Add(match.Count);
            //lt.Add(match2.Count);
            //lt.Add(match3.Count);
            //lt.Add(match4.Count);
            string r = "offerId=\"(\\d+)\"";
            string r2 = "alt=\"([^\"]+)\"";
            string r3 = "(?<= target=\"_blank\" >|rel=\"nofollow\" >)([^<>]+)</a>";
            string f2 = "<li class=\"sm-offerShopwindow\"([^$]+?)</div>\\s+</li>";
            string r4 = "src=\"([^\"]+)\">";
            string r5 = "memberid=\"([^\"]+)\"";

            MatchCollection h = Regex.Matches(org, f2);
            for (int i = 0; i < h.Count; i++)
            {

                DataRow dr1 = dtl.NewRow();

                MatchCollection match = Regex.Matches(h[i].Value, r);
                MatchCollection match2 = Regex.Matches(h[i].Value, r2);
                MatchCollection match3 = Regex.Matches(h[i].Value, r3);
                MatchCollection match4 = Regex.Matches(h[i].Value, r4);
                MatchCollection match5 = Regex.Matches(h[i].Value, r5);
                dr1[0] = match4[0].Groups[1].Value.ToString();


                dr1[1] = match2[0].Groups[1].Value.ToString();


                dr1[2] = match3[0].Groups[1].Value;

                dr1[3] = match[0].Groups[1].Value.ToString();
                dr1[4] = match5[0].Groups[1].Value.ToString();

                dr1[5] = "http://detail.1688.com/offer/" + match[0].Groups[1].Value.ToString() + ".html";
                dtl.Rows.Add(dr1);
            }
            dtl.AcceptChanges();
            return dtl;
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.Value != null & dataGridView1.Focused)
            {
                string s = dataGridView1.CurrentCell.Value.ToString();
                Clipboard.SetDataObject(s);
            }
            if (dataGridView2.CurrentCell.Value != null & dataGridView2.Focused)
            {
                string s = dataGridView2.CurrentCell.Value.ToString();
                Clipboard.SetDataObject(s);
            }
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Focused)
            {
                IDataObject iData = Clipboard.GetDataObject();

                if (iData.GetDataPresent(DataFormats.Text))
                {

               string sl=    (string)iData.GetData(DataFormats.Text);

               dataGridView1.CurrentCell.Value = sl;
                }

                else
                {
                    MessageBox.Show("目前剪贴板中数据不可转换为文本", "错误");

            
            }
        }
            if (dataGridView2.Focused)
            {
                IDataObject iData = Clipboard.GetDataObject();

                if (iData.GetDataPresent(DataFormats.Text))
                {

                    string sl = (string)iData.GetData(DataFormats.Text);

                    dataGridView2.CurrentCell.Value = sl;
                }

                else
                {
                    MessageBox.Show("目前剪贴板中数据不可转换为文本", "错误");


                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataTable gvt1 = GetDgvToTable(dataGridView1);
            string[] strl = new string[] { "false", "true", "[", "]", "{", "}", "￥","CNY"};
            foreach (DataRow c in gvt1.Rows)
            {
                for (int i = 0; i < c.ItemArray.Length;i++ )
                {
                    if (strcontainstrlist(c[i].ToString().ToLower(),strl))
                    {
                        gvt1.Columns.RemoveAt(i);
                        break;
                  }
                }
            }
            dt = gvt1;
            dataGridView1.DataSource = gvt1;
        }
        public bool strcontainstrlist(string f, string[] l)
        {
            bool b = false;
            foreach (string s in l)
            {
                if (f.Contains(s))
                {
                   b = true;
                }
            }
            return b;
        }
    }
}
